2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_spi.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // eusci_b_spi.c - Driver for the eusci_b_spi Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_EUSCI_Bx__
17 #include "eusci_b_spi.h"
18 
19 #include <assert.h>
20 
21 void EUSCI_B_SPI_initMaster (uint16_t baseAddress,
22  EUSCI_B_SPI_initMasterParam *param)
23 {
24  //Disable the USCI Module
25  HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
26 
27  //Reset OFS_UCBxCTLW0 values
28  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCCKPH + UCCKPL + UC7BIT + UCMSB +
29  UCMST + UCMODE_3 + UCSYNC);
30 
31  //Reset OFS_UCBxCTLW0 values
32  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSSEL_3);
33 
34  //Select Clock
35  HWREG16(baseAddress + OFS_UCBxCTLW0) |= (uint16_t)param->selectClockSource;
36 
37  HWREG16(baseAddress + OFS_UCBxBRW) =
38  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
39 
40  /*
41  * Configure as SPI master mode.
42  * Clock phase select, polarity, msb
43  * UCMST = Master mode
44  * UCSYNC = Synchronous mode
45  * UCMODE_0 = 3-pin SPI
46  */
47  HWREG16(baseAddress + OFS_UCBxCTLW0) |= (
48  param->msbFirst +
49  param->clockPhase +
50  param->clockPolarity +
51  UCMST +
52  UCSYNC +
53  param->spiMode
54  );
55 }
56 
57 void EUSCI_B_SPI_select4PinFunctionality (uint16_t baseAddress,
58  uint16_t select4PinFunctionality
59  )
60 {
61  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCSTEM;
62  HWREG16(baseAddress + OFS_UCBxCTLW0) |= select4PinFunctionality;
63 }
64 
65 void EUSCI_B_SPI_changeMasterClock (uint16_t baseAddress,
66  EUSCI_B_SPI_changeMasterClockParam *param)
67 {
68  //Disable the USCI Module
69  HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
70 
71  HWREG16(baseAddress + OFS_UCBxBRW) =
72  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
73 
74  //Reset the UCSWRST bit to enable the USCI Module
75  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSWRST);
76 }
77 
78 void EUSCI_B_SPI_initSlave (uint16_t baseAddress, EUSCI_B_SPI_initSlaveParam *param)
79 {
80  //Disable USCI Module
81  HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
82 
83  //Reset OFS_UCBxCTLW0 register
84  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCMSB +
85  UC7BIT +
86  UCMST +
87  UCCKPL +
88  UCCKPH +
89  UCMODE_3
90  );
91 
92  //Clock polarity, phase select, msbFirst, SYNC, Mode0
93  HWREG16(baseAddress + OFS_UCBxCTLW0) |= (param->clockPhase +
94  param->clockPolarity +
95  param->msbFirst +
96  UCSYNC +
97  param->spiMode
98  );
99 }
100 
101 void EUSCI_B_SPI_changeClockPhasePolarity (uint16_t baseAddress,
102  uint16_t clockPhase,
103  uint16_t clockPolarity
104  )
105 {
106  //Disable the USCI Module
107  HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
108 
109  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCCKPH + UCCKPL);
110 
111  HWREG16(baseAddress + OFS_UCBxCTLW0) |= (
112  clockPhase +
113  clockPolarity
114  );
115 
116  //Reset the UCSWRST bit to enable the USCI Module
117  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSWRST);
118 }
119 
120 void EUSCI_B_SPI_transmitData ( uint16_t baseAddress,
121  uint8_t transmitData
122  )
123 {
124  HWREG16(baseAddress + OFS_UCBxTXBUF) = transmitData;
125 }
126 
127 uint8_t EUSCI_B_SPI_receiveData (uint16_t baseAddress)
128 {
129  return ( HWREG16(baseAddress + OFS_UCBxRXBUF)) ;
130 }
131 
132 void EUSCI_B_SPI_enableInterrupt (uint16_t baseAddress,
133  uint16_t mask
134  )
135 {
136  HWREG16(baseAddress + OFS_UCBxIE) |= mask;
137 }
138 
139 void EUSCI_B_SPI_disableInterrupt (uint16_t baseAddress,
140  uint16_t mask
141  )
142 {
143  HWREG16(baseAddress + OFS_UCBxIE) &= ~mask;
144 }
145 
146 uint8_t EUSCI_B_SPI_getInterruptStatus (uint16_t baseAddress,
147  uint8_t mask
148  )
149 {
150  return ( HWREG16(baseAddress + OFS_UCBxIFG) & mask );
151 }
152 
153 void EUSCI_B_SPI_clearInterrupt (uint16_t baseAddress,
154  uint16_t mask
155  )
156 {
157  HWREG16(baseAddress + OFS_UCBxIFG) &= ~mask;
158 }
159 
160 void EUSCI_B_SPI_enable (uint16_t baseAddress)
161 {
162  //Reset the UCSWRST bit to enable the USCI Module
163  HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~(UCSWRST);
164 }
165 
166 void EUSCI_B_SPI_disable (uint16_t baseAddress)
167 {
168  //Set the UCSWRST bit to disable the USCI Module
169  HWREG16(baseAddress + OFS_UCBxCTLW0) |= UCSWRST;
170 }
171 
172 uint32_t EUSCI_B_SPI_getReceiveBufferAddress (uint16_t baseAddress)
173 {
174  return ( baseAddress + OFS_UCBxRXBUF );
175 }
176 
177 uint32_t EUSCI_B_SPI_getTransmitBufferAddress (uint16_t baseAddress)
178 {
179  return ( baseAddress + OFS_UCBxTXBUF );
180 }
181 
182 uint16_t EUSCI_B_SPI_isBusy (uint16_t baseAddress)
183 {
184  //Return the bus busy status.
185  return (HWREG16(baseAddress + OFS_UCBxSTATW) & UCBUSY);
186 }
187 
188 
189 #endif
190 //*****************************************************************************
191 //
194 //
195 //*****************************************************************************
uint8_t transmitData
MPU_initThreeSegmentsParam param
#define HWREG16(x)
Definition: hw_memmap.h:39